package com.ming.MathTest;

import lombok.Data;

import java.util.*;

/**
 * @Auther: tangC
 * @Date: 2020/7/13 14:26
 * @Description:
 */
public class DemoTest2 {
    private static Stack<Treasure> treasures = new Stack<>();

    public static void main(String[] args) {
        List<Treasure> list = new ArrayList<>();
        list.add(new Treasure());
        list.add(new Treasure());
        list.add(new Treasure());
        list.add(new Treasure());
        list.add(new Treasure());


        List<Treasure> listByPractical = new ArrayList<>(list);
        List<Treasure> listByRare = new ArrayList<>(list);
        SellMethod sellMethod = new SellMethod();
        Collections.sort(listByPractical, new Comparator<Treasure>() {
            @Override
            public int compare(Treasure o1, Treasure o2) {
                return o1.getPractical()-o2.getPractical();
            }
        });

        Collections.sort(listByRare, new Comparator<Treasure>() {
            @Override
            public int compare(Treasure o1, Treasure o2) {
                return o1.getRare()-o2.getRare();
            }
        });

        for (Treasure treasure:listByPractical){
            SellMethod sellMethodTest= new SellMethod();
            sellMethodTest.setTreasures(treasure);
            for (int x = listByPractical.indexOf(treasure);x<listByPractical.size()-1;x++){
                sell(listByPractical.get(x+1),sellMethodTest);
            }
            sellMethodTest.setNumber(sellMethodTest.getTreasures().size());
            if (sellMethodTest.getNumber()>sellMethod.getNumber()){
                sellMethod.setSellMethod(sellMethodTest);
            }
        }
        for (Treasure treasure:listByRare){
            SellMethod sellMethodTest= new SellMethod();
            sellMethodTest.setTreasures(treasure);
            for (int x = listByRare.indexOf(treasure);x<listByRare.size()-1;x++){
                sell(listByRare.get(x+1),sellMethodTest);
            }
            sellMethodTest.setNumber(sellMethodTest.getTreasures().size());
            if (sellMethodTest.getNumber()>sellMethod.getNumber()){
                sellMethod.setSellMethod(sellMethodTest);
            }
        }
        System.out.println(sellMethod.getNumber());
        System.out.println(sellMethod.getTreasures());
        System.out.println("----");
    }

    public static void sell(Treasure treasure,SellMethod sellMethod){
        Treasure peek = sellMethod.peek();
        if (peek.getPractical()<=treasure.getPractical() && peek.getRare()<=treasure.getRare()){
            sellMethod.setTreasures(treasure);
        }
    }
}

@Data
class Treasure {
    private int practical = new Integer( (int) (Math.random() * 10));
    private int rare = new Integer((int) (Math.random() * 10));
}

class SellMethod{
    private int number;
    private Stack<Treasure> treasures = new Stack<>();

    public SellMethod(){

    }
    public void setSellMethod(SellMethod sellMethod){
        this.treasures = sellMethod.getTreasures();
        this.number = sellMethod.number;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public Stack<Treasure> getTreasures() {
        return treasures;
    }
    public Treasure peek(){
        return this.treasures.peek();
    }
    public void setTreasures(Stack<Treasure> treasures) {
        this.treasures = treasures;
    }

    public void setTreasures(Treasure treasures){
        this.getTreasures().push(treasures);
    }
}
